package com.a.module_supplier;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.example.myframe.DistDeptApp;
import com.hjq.http.EasyHttp;
import com.a.lib_bean.api.ParamsApi;
import com.a.lib_bean.bean.CommonSupplierList;
import com.a.lib_bean.bean.Supplier;
import com.a.lib_network.bean.HttpData;
import com.a.lib_network.http.SimpleCallBack;
import com.a.lib_route.supplier.SupplierRouter;
import com.a.lib_utils.TipUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商
 */
@Route(path = SupplierRouter.PATH_MAIN, group = SupplierRouter.GROUP)
public class CommonSupplierActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = CommonSupplierActivity.class.getSimpleName();
    //类型不同显示不同的数据
    public static final char PICK = '1';
    public static final char STOCK = '2';
    private final List<Supplier> supplierList = new ArrayList<>();
    private SmartRefreshLayout supplier_refreshLayout; //下拉刷新
    private AppCompatImageView img_backIv;
    private LinearLayout supplier_ll_search;
    private CommonSupplierAdapter mAdapter;
    private RecyclerView rv_supplier;
    @Autowired
    String url;
    @Autowired
    String routePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_supplier);
        ARouter.getInstance().inject(this);
        LogUtils.i(url +",path="+ routePath);
        initView();
        initListener();
    }


    public void initView() {
        rv_supplier = findViewById(R.id.rv_suppliers);
        supplier_ll_search = findViewById(R.id.supplier_ll_search);
        supplier_refreshLayout = findViewById(R.id.supplier_refreshLayout);
        img_backIv = findViewById(R.id.img_backIv);
        doGetSuppliers();
        rv_supplier.setLayoutManager(new LinearLayoutManager(this));
        supplier_ll_search.setVisibility(View.GONE);
    }

    public void initListener() {
        img_backIv.setOnClickListener(this);
        supplier_ll_search.setOnClickListener(this);
        //设置下拉刷新和上拉加载监听
        supplier_refreshLayout.setOnRefreshListener(refreshLayout -> {
                    doGetSuppliers();
                    mAdapter.notifyDataSetChanged();
                    refreshLayout.finishRefresh();
                }
        );
    }


    private void doGetSuppliers() {
        Map<String, String> params = new HashMap<>();
        params.put("distDeptID", DistDeptApp.distDeptID);
//        String url;
//        if (type == '1') url = Url.doGetOutsourcesFactory;
//        else url = Url.doGetOutsourcesStockFactory;
        EasyHttp.get(this).api(new ParamsApi(url, params))
                .request(new SimpleCallBack<HttpData<CommonSupplierList>>(CommonSupplierActivity.this) {
                    @Override
                    public void succeed(HttpData<CommonSupplierList> result) {
                        if (!result.isSuccess()) {
                            TipUtil.toast(result.getMsg());
                            finish();
                            return;
                        }
                        Log.i(TAG, "succeed: " + result.getData().getDataList());
//                        Log.i(TAG, "succeed2: " + result.getData().getString("dataList"));
                        if (supplierList.size() > 0)
                            supplierList.clear();
                        supplierList.addAll(result.getData().getDataList());
                        if (mAdapter == null) {
                            mAdapter = new CommonSupplierAdapter(supplierList);
                            mAdapter.setOnItemClickListener((adapter, view, position) ->
                            {
//                                final Supplier supplier = supplierList.get(position);
                                ARouter.getInstance().build(routePath).withObject("supplier", supplierList.get(position)).navigation();
//                                if (type == '1')
//                                    OutsourcesPendingActivity.actionStart(OutsourcesSupplierActivity.this,
//                                            supplier.getProviderID(), supplier.getWhid());
//                                else
//                                    OutsourcesStockActivity.actionStart(OutsourcesSupplierActivity.this, supplier.getProviderID(), supplier.getWhid());
                            });
                            rv_supplier.setAdapter(mAdapter);
                        }
                    }
                });
    }

    public static void actionStart(Context context, char type) {
        Intent intent = new Intent(context, CommonSupplierActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.img_backIv) {
            finish();
        }
    }

}
